package com.imooc.activitiweb.controller;

import com.imooc.activitiweb.SecurityUtil;
import com.imooc.activitiweb.mapper.ActivitiMapper;
import com.imooc.activitiweb.pojo.UserInfoBean;
import com.imooc.activitiweb.util.AjaxResponse;
import com.imooc.activitiweb.util.GlobalConfig;
import org.activiti.bpmn.model.Process;
import org.activiti.bpmn.model.*;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/processForm")
public class ProcessFormController {

    @Autowired
    ActivitiMapper mapper;

    /**
     * 查询表单模板数据
     * @return
     */
    @GetMapping(value = "/selectFormModels")
    public AjaxResponse selectFormModels() {
        try {

            List<HashMap<String,Object>> formModelList = mapper.selectHxFormModel();

            return AjaxResponse.AjaxData(GlobalConfig.ResponseCode.SUCCESS.getCode(),
                    GlobalConfig.ResponseCode.SUCCESS.getDesc(), formModelList);
        } catch (Exception e) {
            return AjaxResponse.AjaxData(GlobalConfig.ResponseCode.ERROR.getCode(),
                    "查询表单模板列表", e.toString());
        }

    }


    /**
     * 根据租户ID、表单key，获取表单模板
     * @return
     */
    @GetMapping(value = "/getFormModel")
    public AjaxResponse getFormModel(@RequestParam String tenantId,
                                     @RequestParam String formKey) {
        try {

            HashMap<String,Object> formModel = mapper.getHxFormModel(tenantId,formKey);

            return AjaxResponse.AjaxData(GlobalConfig.ResponseCode.SUCCESS.getCode(),
                    GlobalConfig.ResponseCode.SUCCESS.getDesc(), formModel);
        } catch (Exception e) {
            return AjaxResponse.AjaxData(GlobalConfig.ResponseCode.ERROR.getCode(),
                    "获取表单模板失败", e.toString());
        }

    }


    /**
     * 查询表单业务数据列表
     * @return
     */
    @GetMapping(value = "/selectFormDatas")
    public AjaxResponse selectFormDatas() {
        try {

            List<HashMap<String,Object>> formDataList = mapper.selectHxFormData();

            return AjaxResponse.AjaxData(GlobalConfig.ResponseCode.SUCCESS.getCode(),
                    GlobalConfig.ResponseCode.SUCCESS.getDesc(), formDataList);
        } catch (Exception e) {
            return AjaxResponse.AjaxData(GlobalConfig.ResponseCode.ERROR.getCode(),
                    "查询表单业务列表", e.toString());
        }

    }


    /**
     * 根据租户ID、任务ID、表单key，获取表单业务数据
     * @return
     */
    @GetMapping(value = "/getFormData")
    public AjaxResponse getFormData(@RequestParam String tenantId,
                                    @RequestParam String taskId,
                                     @RequestParam String formKey) {
        try {
            HashMap<String,Object> formData = mapper.getHxFormData(taskId,formKey);

            return AjaxResponse.AjaxData(GlobalConfig.ResponseCode.SUCCESS.getCode(),
                    GlobalConfig.ResponseCode.SUCCESS.getDesc(), formData);
        } catch (Exception e) {
            return AjaxResponse.AjaxData(GlobalConfig.ResponseCode.ERROR.getCode(),
                    "获取表单业务数据失败", e.toString());
        }

    }

}
